package cn.util;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

public class ManifestParser {

    private final List<Model> sManifest = new ArrayList<>();
    private String manifestPath = null;

    public ManifestParser(String manifestPath) {
        this.manifestPath = manifestPath;
    }
    public static List<Model>  loadManifest(String manifestPath){
        return new ManifestParser(manifestPath).parse();
    }
    private List<Model> parse() {
        try {
            XmlPullParserFactory parserFactory = XmlPullParserFactory.newInstance();
            if(parserFactory==null){
                return null;
            }
            XmlPullParser parser = parserFactory.newPullParser();
            parser.setInput(new FileReader(this.manifestPath));

            return startParse(parser);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    private List<Model> startParse(XmlPullParser xmlPullParser) {
        if(xmlPullParser==null){
            return null;
        }

        List<Model> models = new ArrayList<>();
        try {
            //开始
            int eventType = xmlPullParser.getEventType();
            Model model = null;
            while (eventType != XmlPullParser.END_DOCUMENT) {
                String nodeName = xmlPullParser.getName();
                switch (eventType) {
                    //文档开始
                    case XmlPullParser.START_DOCUMENT:
                        System.out.println("文档解析开始");
                        break;
                    //开始节点
                    case XmlPullParser.START_TAG:
                         if(!nodeName.equals("activity") &&!nodeName.equals("service")) {
                            break;
                         }

                        int count = xmlPullParser.getAttributeCount();
                        for (int i = 0; i < count; i++) {
                            String name = xmlPullParser.getAttributeName(i);
                            String namespace = xmlPullParser.getAttributeNamespace(i);
                            if(!name.equals("android:name")){
                                continue;
                            }
                            String androidName = xmlPullParser.getAttributeValue(namespace,name);

                            model = new Model();
                            model.name = androidName;
                            model.type  = nodeName;

                            break;
                        }

                        break;
                    //结束节点
                    case XmlPullParser.END_TAG:
                        if(model!=null){
                            models.add(model);
                            System.out.println(model);
                            model = null;
                        }
                        break;

                    case XmlPullParser.END_DOCUMENT:
                        System.out.println("文档解析完成");
                        break;
                    default:
                        break;
                }
                eventType = xmlPullParser.next();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return models;

    }
}
